<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\command;

use app\enum\CommonEnum;
use app\model\CrontabTaskModel;
use app\service\CrontabTaskService;
use app\service\material\MaterialPicCostService;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class OePicClientCostWork extends BaseWork implements DailyStatWork
{
    public ?int $tag = null;

    public function setTag($tag = null): bool
    {
        if (! is_null($tag)) {
            $this->tag = $tag;
        }
        return true;
    }

    public function getTag(): ?string
    {
        return strval($this->tag);
    }

    public function getCommandName(): string
    {
        return $this->getName();
    }

    /**
     * 获取定时任务.
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getCrontabTaskInfo(): CrontabTaskModel
    {
        $where = [
            ['task_name', '=', $this->getName()],
            ['tag', '=', $this->getTag()],
        ];

        $obj = new CrontabTaskModel();
        $model = $obj->where($where)->field(true)->find();
        return $model instanceof CrontabTaskModel ? $model : $obj;
    }

    protected function configure()
    {
        // 指令配置
        $this->setName('oe_pic_client_cost_work');
        $this->setDescription('缓存客户图片素材消耗')
            ->addArgument('num', Argument::REQUIRED, '序号')
            ->addArgument('type', Argument::REQUIRED, '类型  1：昨日 2：自定义时间')
            ->addArgument('start_date', Argument::OPTIONAL, '开始日期 Y-m-d')
            ->addArgument('end_date', Argument::OPTIONAL, '结束日期 Y-m-d')
            ->setHelp('php think oe_pic_client_cost_work [num] [type] [start_date] [end_date]');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        $num = $input->getArgument('num');
        $type = $input->getArgument('type');
        $start = $input->getArgument('start_date');
        $end = $input->getArgument('end_date');
        $this->setTag(intval($num));
        // 昨日    如果是昨日，判断是数据是否已经拉取或者当日已执行
        if ($type == 1) {
            $dateInt = date('Ymd', strtotime('-1 day'));
            // 获取凌晨的时间戳
            $time = strtotime(date('Y-m-d 00:00:00'));
            // 判断比例的是否跑完了
            $lastTime = CrontabTaskModel::where('task_name', 'cache_client_cash_rate_work')
                ->where('tag', CommonEnum::OE_MEDIA)
                ->where('finish_time', '>', $time)
                ->find();
            if (! $lastTime) {
                exit;
            }
            // 判断数据是否跑完了
//            $isDone = TaskAccountService::isTodayTaskDone(ServiceCostEnum::TASK_TYPE_PICTURE, CommonEnum::OE_MEDIA, intval($dateInt));
//            if (! $isDone) {
//                exit;
//            }
            $lastTime = CrontabTaskModel::where('task_name', 'media_cost_pull_work')
                ->where('tag', '1-3')
                ->where('finish_time', '>', $time)
                ->find();
            if (! $lastTime) {
                exit;
            }
        }
        $output->writeln('=======缓存客户素材巨量消耗开始' . date('Y-m-d H:i:s', time()) . '=======');
        if ($type == 2) {
            $dates = createDateRange($start, $end);
            foreach ($dates as $dateInt) {
                $dateInt = date('Ymd', strtotime($dateInt));
                for ($i = 0; $i < 10; ++$i) {
                    $res = MaterialPicCostService::getClientMaterialCost(CommonEnum::OE_MEDIA, (int) $dateInt, (int) $i);
                    if ($res) {
                        echo $dateInt . '-' . $i . '-OK' . PHP_EOL;
                    }
                }
            }
        } else {
            $res = MaterialPicCostService::getClientMaterialCost(CommonEnum::OE_MEDIA, (int) $dateInt, (int) $num);
        }
        // 如果是执行昨日的，更新任务表执行时间
        if ($type == 1 && $res) {
            CrontabTaskService::setSingleTask($this);
        }
        $output->writeln('=======缓存客户素材巨量消耗结束' . date('Y-m-d H:i:s', time()) . '=======');
    }
}
